package org.hotswap.agent.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.Reader;

/**
 * @author luzhiqing
 * @version 1.0
 * @date 2022/10/21
 */
public class FileUtils {
    public static String read(String path) {
        File file = new File(path);
        String content = "";
        FileReader fileReader = null;
        BufferedReader bufferedReader = null;
        try {
            fileReader = new FileReader(file);
            bufferedReader = new BufferedReader(fileReader);
            String line;
            while (null != (line = bufferedReader.readLine())) {
                content = content + line;
            }
        } catch (Exception e) {
            throw new RuntimeException("文件读取异常");
        } finally {
            silentClose(fileReader);
            silentClose(bufferedReader);
        }
        return content;
    }

    public static void silentClose(Reader reader) {
        if (null == reader) {
            return;
        }
        try {
            reader.close();
        } catch (Exception e) {

        }
    }
}
